package management_system;

import java.util.*;

public class Student {
    private String id;
    private String name;
    private List<Score> scores = new ArrayList<>();
    public Student(String id, String name) { this.id = id; this.name = name; }
    public String getId()   { return id; }
    public String getName() { return name; }
    public void addScore(Score score) { scores.add(score); }

    /* 只统计合法成绩 */
    public double getGPA() {
        double totalPoints = 0.0, totalCredits = 0.0;
        for (Score s : scores) {
            totalPoints += s.getPoint() * s.getCourse().getCredit();
            totalCredits += s.getCourse().getCredit();
        }
        return totalCredits == 0 ? 0 : totalPoints / totalCredits;
    }
    public Score getScoreByCourseName(String courseName) {
        for (Score s : scores)
            if (s.getCourse().getName().equalsIgnoreCase(courseName))
                return s;
        return null;
    }
    @Override
    public String toString() {
        return "学号: " + id + ", 姓名: " + name + ", GPA: " + String.format("%.2f", getGPA());
    }
}